package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Repository;

import dao.PersonDao;
import oracle.jdbc.proxy.annotation.Pre;
import pojo.Person;
import util.DBFactory;
@Repository(value="personDao")
public class PersonDaoImpl implements PersonDao {
	private Connection connection ;
	public PersonDaoImpl() {
		connection = DBFactory.getInstance();
		System.out.println("personDao init success....");
	}
	@Override
	public Person addPerson(Person person) {
		String sql = "insert into persons(id,name,sex,sal,birth) values(?,?,?,?,?)";
		PreparedStatement pst = null;
		int result = 0;
		
		try {
			person.setId(UUID.randomUUID().toString());
			pst = connection.prepareStatement(sql);
			pst.setString(1, person.getId());
			pst.setString(2, person.getName());
			pst.setBoolean(3, person.isSex());
			pst.setInt(4, person.getSal());
			pst.setDate(5, new Date(person.getBirth().getTime()));
			result = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("add action failed...");
			return null;
		}
		System.out.println("add person:"+person.toString());
		return person;
	}

	@Override
	public int deletePerson(String id) {
		String sql = "delete from persons where id = ?";
		PreparedStatement pst = null;
		int result = 0;
		
		try {
			pst = DBFactory.getInstance().prepareStatement(sql);
			pst.setString(1, id);
			result = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("delete action failed");
			result = 0;
		}
		System.out.println("delete person id:"+id);
		return result;
	}

	@Override
	public Person updatePerson(Person person) {
		String sql = "update persons set name = ?, sex = ?, sal = ?, birth = ? where id = ?";
		PreparedStatement pst = null;
		int result = 0;
		
		try {
			pst = DBFactory.getInstance().prepareStatement(sql);
			pst.setString(1, person.getName());
			pst.setBoolean(2, person.isSex());
			pst.setInt(3, person.getSal());
			pst.setDate(4, new Date(person.getBirth().getTime()));
			pst.setString(5, person.getId());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("update action failed...");
			return null;
		}
		System.out.println("update person:"+person.toString());
		return person;
	}

	@Override
	public List<Person> findAllPerson() {
		String sql = "select * from persons";
		List<Person> persons = new ArrayList<Person>();
		PreparedStatement pst = null;
		ResultSet rSet = null;
		int result = 0;
		
		try {
			pst = DBFactory.getInstance().prepareStatement(sql);
			rSet= pst.executeQuery();
			while(rSet.next()){
				Person person = new Person();
				person.setId(rSet.getString("id"));
				person.setName(rSet.getString("name"));
				person.setSex(new Boolean(rSet.getString("sex")));
				person.setSal(rSet.getInt("sal"));
				persons.add(person);
			}
		} catch (SQLException e) {
			System.out.println("findAll action failed...");
			return null;
		}
		for(int i=0;i<persons.size();i++)
			System.out.println("find a  person:"+persons.get(i).toString());
		return persons;
	}

}
